from django.db import models


# Create your models here.
# 策略表
class TStrategy(models.Model):
    StgID = models.IntegerField(
        db_column="StgID",
        primary_key=True,
        verbose_name="策略代码"
    )
    StgName = models.CharField(
        db_column="StgName",
        max_length=50,
        verbose_name="策略名称"
    )
    StgType = models.CharField(
        db_column="StgType",
        max_length=1,
        verbose_name="策略类型"
    )
    CreateTime = models.CharField(
        db_column="CreateTime",
        max_length=19,
        verbose_name="创建时间"
    )
    BackUpdateTime = models.CharField(
        db_column="BackUpdateTime",
        max_length=19,
        null=True,
        verbose_name="快速回测最后运行时间"
    )
    SimUpdateTime = models.CharField(
        db_column="SimUpdateTime",
        null=True,
        max_length=19,
        verbose_name="模拟最后运行时间"
    )
    BackTest = models.IntegerField(
        db_column="BackTest",
        verbose_name="快速回测次数"
    )
    Sim = models.IntegerField(
        db_column="Sim",
        verbose_name="模拟次数"
    )
    Status = models.CharField(
        db_column="Status",
        max_length=1,
        verbose_name="策略状态"
    )
    CopyStgID = models.IntegerField(
        db_column="CopyStgID",
        null=True,
        verbose_name="复制实盘的策略代码"
    )
    CustomerID = models.CharField(
        db_column="CustomerID",
        max_length=20,
        verbose_name="用户代码"
    )

    class Meta:
        db_table = 't_strategy'


# 策略回测表
class TBackapproval(models.Model):
    """
    新增和打包落库
    """
    stgid = models.IntegerField(db_column='StgID', primary_key=True)  # Field name made lowercase.
    stgfile = models.TextField(db_column='StgFile')  # Field name made lowercase.
    confile = models.TextField(db_column='ConFile')  # Field name made lowercase.
    stgpara = models.CharField(db_column='StgPara', max_length=2000)  # Field name made lowercase.
    envid = models.CharField(db_column='EnvID', max_length=20)  # Field name made lowercase.
    remark = models.CharField(db_column='Remark', max_length=500)  # Field name made lowercase.
    updatetime = models.DateTimeField(db_column='UpdateTime')  # Field name made lowercase.
    status = models.CharField(db_column='Status', max_length=1)  # Field name made lowercase.
    auditmsg = models.CharField(db_column='AuditMsg', max_length=2000)  # Field name made lowercase.
    packstatus = models.CharField(db_column='PackStatus', max_length=1)  # Field name made lowercase.
    packmsg = models.CharField(db_column='PackMsg', max_length=2000)  # Field name made lowercase.
    packtime = models.DateTimeField(db_column='PackTime')  # Field name made lowercase.
    runpath = models.CharField(db_column='RunPath', max_length=1000, blank=True,
                               null=True)  # Field name made lowercase.

    class Meta:
        managed = False
        db_table = 't_backapproval'


class TBackposition(models.Model):
    stgid = models.IntegerField(db_column='StgID', primary_key=True)  # Field name made lowercase.
    batchid = models.IntegerField(db_column='BatchID')  # Field name made lowercase.
    accountid = models.CharField(db_column='AccountID', max_length=32)  # Field name made lowercase.
    symbol = models.CharField(db_column='Symbol', max_length=30)  # Field name made lowercase.
    tradingday = models.CharField(db_column='TradingDay', max_length=8)  # Field name made lowercase.
    exchid = models.CharField(db_column='ExchID', max_length=10)  # Field name made lowercase.
    closepx = models.FloatField(db_column='ClosePx')  # Field name made lowercase.
    buyqty = models.BigIntegerField(db_column='BuyQty')  # Field name made lowercase.
    buyopenpx = models.FloatField(db_column='BuyOpenPx')  # Field name made lowercase.
    buymargin = models.FloatField(db_column='BuyMargin')  # Field name made lowercase.
    buypnl = models.FloatField(db_column='BuyPNL')  # Field name made lowercase.
    sellqty = models.BigIntegerField(db_column='SellQty')  # Field name made lowercase.
    sellopenpx = models.FloatField(db_column='SellOpenPx')  # Field name made lowercase.
    sellmargin = models.FloatField(db_column='SellMargin')  # Field name made lowercase.
    sellpnl = models.FloatField(db_column='SellPNL')  # Field name made lowercase.

    class Meta:
        managed = False
        db_table = 't_backposition'
        unique_together = (('stgid', 'batchid', 'accountid', 'symbol', 'tradingday'),)


# 净值曲线
class TBackreturn(models.Model):
    stgid = models.IntegerField(db_column='StgID', primary_key=True)  # Field name made lowercase.
    batchid = models.IntegerField(db_column='BatchID')  # Field name made lowercase.
    data = models.TextField()
    date = models.TextField()
    heatyear = models.TextField(db_column='heatYear')  # Field name made lowercase.
    heatdata = models.TextField(db_column='heatData')  # Field name made lowercase.
    yearlyretdate = models.TextField(db_column='yearlyRetDate')  # Field name made lowercase.
    yearlyretdata = models.TextField(db_column='yearlyRetData')  # Field name made lowercase.
    monthlyretdistdata = models.TextField(db_column='monthlyRetDistData')  # Field name made lowercase.
    boxdata = models.TextField(db_column='boxData')  # Field name made lowercase.

    class Meta:
        managed = False
        db_table = 't_backreturn'
        unique_together = (('stgid', 'batchid'),)


# 策略运行表
class TBackrun(models.Model):
    id = models.AutoField(db_column='ID', primary_key=True)  # Field name made lowercase.
    stgid = models.IntegerField(db_column='StgID')  # Field name made lowercase.
    batchid = models.IntegerField(db_column='BatchID')  # Field name made lowercase.
    starttime = models.CharField(db_column='StartTime', max_length=19)  # Field name made lowercase.
    endtime = models.CharField(db_column='EndTime', max_length=19)  # Field name made lowercase.
    frequency = models.CharField(db_column='Frequency', max_length=1)  # Field name made lowercase.
    region = models.CharField(db_column='Region', max_length=50)  # Field name made lowercase.
    runstatus = models.CharField(db_column='RunStatus', max_length=1)  # Field name made lowercase.
    envid = models.CharField(db_column="EnvID", max_length=20)
    runpath = models.CharField(db_column="RunPath", max_length=1000)

    class Meta:
        managed = False
        db_table = 't_backrun'
        unique_together = (('id', 'stgid', 'batchid'),)


# 收益概览
class TBacksummary(models.Model):
    stgid = models.IntegerField(db_column='StgID', primary_key=True)  # Field name made lowercase.
    batchid = models.IntegerField(db_column='BatchID')  # Field name made lowercase.
    simtime = models.CharField(db_column='SimTime', max_length=19)  # Field name made lowercase.
    simstartdate = models.CharField(db_column='SimStartDate', max_length=10)  # Field name made lowercase.
    simenddate = models.CharField(db_column='SimEndDate', max_length=10)  # Field name made lowercase.
    totaldays = models.IntegerField(db_column='TotalDays')  # Field name made lowercase.
    pl = models.FloatField(db_column='PL')  # Field name made lowercase.
    maxdrawndown = models.FloatField(db_column='MaxDrawndown')  # Field name made lowercase.
    maxdrawndowndays = models.IntegerField(db_column='MaxDrawndownDays')  # Field name made lowercase.
    sharpratio = models.FloatField(db_column='SharpRatio')  # Field name made lowercase.
    calmarratio = models.FloatField(db_column='CalmarRatio')  # Field name made lowercase.
    windays = models.IntegerField(db_column='WinDays')  # Field name made lowercase.
    lossdays = models.IntegerField(db_column='LossDays')  # Field name made lowercase.
    windaysrate = models.FloatField(db_column='WinDaysRate')  # Field name made lowercase.
    totaltrans = models.IntegerField(db_column='TotalTrans')  # Field name made lowercase.
    wintrans = models.IntegerField(db_column='WinTrans')  # Field name made lowercase.
    losstrans = models.IntegerField(db_column='LossTrans')  # Field name made lowercase.
    wintransrate = models.FloatField(db_column='WinTransRate')  # Field name made lowercase.
    avgtradeprofit = models.FloatField(db_column='AvgTradeProfit')  # Field name made lowercase.
    avgdailyprofit = models.FloatField(db_column='AvgDailyProfit')  # Field name made lowercase.
    beginnetvalue = models.FloatField(db_column='BeginNetValue')  # Field name made lowercase.
    endnetvalue = models.FloatField(db_column='EndNetValue')  # Field name made lowercase.
    summaryscore = models.IntegerField(db_column='SummaryScore')  # Field name made lowercase.
    annualret = models.FloatField(db_column='AnnualRet')  # Field name made lowercase.
    profitscore = models.IntegerField(db_column='ProfitScore')  # Field name made lowercase.
    costperfscore = models.IntegerField(db_column='CostPerfScore')  # Field name made lowercase.
    stabscore = models.IntegerField(db_column='StabScore')  # Field name made lowercase.
    capitalscore = models.IntegerField(db_column='CapitalScore')  # Field name made lowercase.
    sensitivityscore = models.IntegerField(db_column='SensitivityScore')  # Field name made lowercase.
    annualvolatility = models.FloatField(db_column='AnnualVolatility')  # Field name made lowercase.
    stabilityoftimeseries = models.FloatField(db_column='StabilityOfTimeseries')  # Field name made lowercase.
    omegaratio = models.FloatField(db_column='OmegaRatio')  # Field name made lowercase.
    sortinoratio = models.FloatField(db_column='SortinoRatio')  # Field name made lowercase.
    skew = models.FloatField(db_column='Skew')  # Field name made lowercase.
    kurtosis = models.FloatField(db_column='Kurtosis')  # Field name made lowercase.
    tailratio = models.FloatField(db_column='TailRatio')  # Field name made lowercase.
    commonsenseratio = models.FloatField(db_column='CommonSenseRatio')  # Field name made lowercase.
    cumreturnsfinal = models.FloatField(db_column='CumReturnsFinal')  # Field name made lowercase.

    class Meta:
        managed = False
        db_table = 't_backsummary'
        unique_together = (('stgid', 'batchid'),)


class TBacktrade(models.Model):
    stgid = models.IntegerField(db_column='StgID')  # Field name made lowercase.
    batchid = models.IntegerField(db_column='BatchID')  # Field name made lowercase.
    accountid = models.CharField(db_column='AccountID', max_length=32)  # Field name made lowercase.
    symbol = models.CharField(db_column='Symbol', max_length=30)  # Field name made lowercase.
    tradingday = models.CharField(db_column='TradingDay', max_length=8)  # Field name made lowercase.
    exchid = models.CharField(db_column='ExchID', max_length=10)  # Field name made lowercase.
    direction = models.CharField(db_column='Direction', max_length=1)  # Field name made lowercase.
    offset = models.CharField(db_column='Offset', max_length=1)  # Field name made lowercase.
    tradepx = models.DecimalField(db_column='TradePx', max_digits=19, decimal_places=4)  # Field name made lowercase.
    tradeytm = models.DecimalField(db_column='TradeYTM', max_digits=19, decimal_places=4, blank=True,
                                   null=True)  # Field name made lowercase.
    qty = models.BigIntegerField(db_column='Qty')  # Field name made lowercase.
    openpx = models.DecimalField(db_column='OpenPx', max_digits=19, decimal_places=4)  # Field name made lowercase.
    tradetime = models.DateTimeField(db_column='TradeTime')  # Field name made lowercase.

    class Meta:
        managed = False
        db_table = 't_backtrade'


class TEnvironment(models.Model):
    envid = models.AutoField(db_column='EnvID', primary_key=True)  # Field name made lowercase.
    envname = models.CharField(db_column='EnvName', max_length=100)  # Field name made lowercase.
    ip = models.CharField(db_column='IP', max_length=40)  # Field name made lowercase.
    updatetime = models.CharField(db_column='UpdateTime', max_length=19)  # Field name made lowercase.
    type = models.CharField(db_column='Type', max_length=1)  # Field name made lowercase.
    backtype = models.CharField(db_column='BackType', max_length=1)  # Field name made lowercase.
    runpath = models.CharField(db_column='RunPath', max_length=1000)  # Field name made lowercase.
    remark = models.CharField(db_column='Remark', max_length=500)  # Field name made lowercase.

    class Meta:
        managed = False
        db_table = 't_environment'
